//: [Previous](@previous)

import UIKit
import CoreGraphics
import CoreImage
import PlaygroundSupport

extension UIImage {
    
    func maskWithColor(color: UIColor) -> UIImage? {
        let maskImage = cgImage!
        
        let width = size.width
        let height = size.height
        let bounds = CGRect(x: 0, y: 0, width: width, height: height)
        
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!
        
        context.clip(to: bounds, mask: maskImage)
        context.setFillColor(color.cgColor)
        context.fill(bounds)
        
        if let cgImage = context.makeImage() {
            let coloredImage = UIImage(cgImage: cgImage)
            return coloredImage
        } else {
            return nil
        }
    }
}

extension UIImage {
    
    /**
     Return a new image in shades of gray + alpha
     */
    func convertToGrayScale() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let imageRect = CGRect(x: 0.0, y: 0.0, width: self.size.width, height: self.size.height)
        let context = UIGraphicsGetCurrentContext()
        
        // Draw a white background
        context!.setFillColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        context!.fill(imageRect)
        
        // optional: increase contrast with colorDodge before applying luminosity
        // (my images were too dark when using just luminosity - you may not need this)
        self.draw(in: imageRect, blendMode: CGBlendMode.colorDodge, alpha: 0.7)
        
        
        // Draw the luminosity on top of the white background to get grayscale of original image
        self.draw(in: imageRect, blendMode: CGBlendMode.luminosity, alpha: 0.90)
        
        // optional: re-apply alpha if your image has transparency - based on user1978534's answer (I haven't tested this as I didn't have transparency - I just know this would be the the syntax)
        // self.draw(in: imageRect, blendMode: CGBlendMode.destinationIn, alpha: 1.0)
        
        let grayscaleImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return grayscaleImage!
    }
    
    
    func getGrayScale() -> UIImage {
        let ciImage = CIImage(image: self)
        let grayscale = ciImage!.applyingFilter("CIColorControls", withInputParameters: [ kCIInputSaturationKey: 0.0, kCIInputBrightnessKey: 0.2])
        let img = UIImage(ciImage: grayscale, scale: scale, orientation: imageOrientation)
        return img
        
    }
    
    func getLight() -> UIImage {
        let ciImage = CIImage(image: self)
        let whiteAlpha = ciImage!.applyingFilter("CIColorControls", withInputParameters: [ kCIInputSaturationKey: 0.6,
                                                                                           kCIInputBrightnessKey: 0.2])
        let img = UIImage(ciImage: whiteAlpha, scale: scale, orientation: imageOrientation)
        return img
        
    }
}


let image = #imageLiteral(resourceName: "Bundesliga_League.png").getLight()
let imageWhite = UIImageView(image: image)

let view = UIView()
view.frame = CGRect(origin: .zero
    , size: CGSize(width: 200, height: 200))
view.addSubview(imageWhite)

let imageGray = UIImageView(frame: CGRect(origin: CGPoint(x: 68, y: 0), size: CGSize(width: 65, height: 65)))
imageGray.image = #imageLiteral(resourceName: "Bundesliga_League.png").getGrayScale()
view.addSubview(imageGray)


let imageSample = UIImageView(frame: CGRect(origin: CGPoint(x: 136, y: 0), size: CGSize(width: 65, height: 65)))
imageSample.image = #imageLiteral(resourceName: "Bundesliga_League.png")
view.addSubview(imageSample)


let gradientMask = CAGradientLayer()
gradientMask.backgroundColor = UIColor.clear.cgColor
gradientMask.frame = imageSample.bounds
gradientMask.startPoint = CGPoint(x: 0.0, y: 0.5);
gradientMask.endPoint = CGPoint(x: 1.0, y: 0.5);
gradientMask.backgroundColor = UIColor.clear.cgColor
gradientMask.colors = [UIColor.white.cgColor, UIColor.white.withAlphaComponent(0.1).cgColor, UIColor.white.withAlphaComponent(0.1).cgColor, UIColor.white.cgColor]
gradientMask.locations = [0.0,0.25,0.75, 1.0]
imageSample.layer.addSublayer(gradientMask)

PlaygroundPage.current.liveView = view

//: [Next](@next)
